<?php
	require_once dirname (BASEPATH) . '/application/controllers/system/_init.php';
	require_once dirname (BASEPATH) . '/application/libraries/ImageUpload.php';
	
	class abs_admin extends _init{
		function index(){
			if ($this->check_admin_session() === 1){				
				$this->main();
			} else {
				$this->template->set_template('default');
				$this->template->write('content_1', $this->load->view('admin/frm_admin_login', null, true));
				$this->template->render();
			}
		}
		
		private function check_admin_session(){
			$logged_in = $this->session->userdata('admin_logged_in');
						
			if (!empty($logged_in) && $logged_in)
				return 1;			
			else
				return 0;			
		}
		private function pre_admin_data($active_menu=''){
			$this->template->set_template('right_column');		
			
			$data_content['username'] = $this->session->userdata('name');
			$data_menu['active_menu'] = $active_menu;
			
			$this->template->write('menu_top', $this->load->view('template/component/admin/frm_admin_nav', $data_content, true));
			$this->template->write('right_sidebar', $this->load->view('template/component/admin/frm_admin_menu_left', $data_menu, true));			
		}
		
		// menu section
		function main(){
			if ($this->check_admin_session() === 1){				
				$this->pre_admin_data('main');		
														
				$this->template->write('content_1', $this->load->view('admin/frm_admin_main', null, true));
				$this->template->render();
			} else
				redirect(base_url() . 'admin');
		}
		function cafe(){
			$opr = $this->uri->segment(3);
			switch ($opr){
				case 'list': default:{
					$opr = $this->uri->segment(4);
					switch ($opr){
						case 'ajax_remove_item':{
							$result = false;
							$message = null;
							$redirect = null;
							
							$this->load->library('define/operation_message');
							if (!$this->check_admin_session()){
								$result = -1;
								$message = $this->operation_message->msg_relogin;
								$redirect = base_url() . 'admin';
							} else {
								$this->load->model('local/data/cafe/mod_cafe');								
								
								if ($this->mod_cafe->remove($this->input->post('c'))){
									$result = true;
									$message = $this->operation_message->msg_remove_success;
								} else
									$message = $this->operation_message->msg_remove_fail;
							}
								
							print_r(json_encode(array('result' => $result,
														'message' => $message)));
						}
						break;
						case 'ajax_enable_cafe':{
							$result = false;
							$message = null;
							$redirect = null;
														
							$this->load->library('define/operation_message');
							if (!$this->check_admin_session()){
								$result = -1;
								$message = $this->operation_message->msg_relogin;
								$redirect = base_url() . 'admin';								
							} else {
								$this->load->model('local/data/cafe/mod_cafe');
								if ($this->mod_cafe->enable_cafe($this->input->post('c'))){
									$result = true;
									$message = $this->operation_message->msg_save_success;
								} else								
									$message = $this->operation_message->msg_save_fail;
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect' => $redirect)));
						}
						break;
						case 'ajax_disable_cafe':{
							$result = false;
							$message = null;
							$redirect = null;
														
							$this->load->library('define/operation_message');
							if (!$this->check_admin_session()){
								$result = -1;
								$message = $this->operation_message->msg_relogin;
								$redirect = base_url() . 'admin';								
							} else {
								$this->load->model('local/data/cafe/mod_cafe');
								if ($this->mod_cafe->disable_cafe($this->input->post('c'))){
									$result = true;
									$message = $this->operation_message->msg_save_success;
								} else								
									$message = $this->operation_message->msg_save_fail;
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect' => $redirect)));
						}
						break;
						default:{
							if ($this->check_admin_session() === 1){
								$this->pre_admin_data('cafe');
								
								$start_index = $this->input->get('s');								
								$this->load->library('define/general_define');
								$display_length = $this->general_define->paginate_display_length;							
															
								if (empty($start_index))
									$start_index = 1;
								
								$start_index = (($start_index-1) * $display_length);
														
								$this->load->model('local/data/cafe/mod_cafe');
								$data_content['cafe_list'] = $this->mod_cafe->get_cafe_list($display_length, $start_index, array('cafe.CAF_ID','CAF_NAME','CAF_TITLE','CAF_IMG_L_FILENAME','MEM_EMAIL'));							
								$data_content['page_count'] = ceil($this->mod_cafe->get_count() / $display_length);
								$data_content['page_start_index'] = $start_index;

								$this->template->write('content_1', $this->load->view('admin/frm_admin_cafe', $data_content, true));
								$this->template->render();
							} else 
								redirect(base_url() . 'admin/logout');
						}
						break;
					}														
				}
				break;
				case 'gallery':{
					if ($this->check_admin_session() === 1){
						$this->pre_admin_data('cafe');
								
						$start_index = $this->input->get('s');								
						$this->load->library('define/general_define');
						$display_length = $this->general_define->paginate_display_length;							
															
						if (empty($start_index))
							$start_index = 1;
						
						$start_index = (($start_index-1) * $display_length);
												
						$this->load->model('local/data/cafe/mod_cafe_img_gallery');		
						$data_content['data'] = $this->mod_cafe_img_gallery->get_gallery_list();											
						$data_content['page_count'] = ceil($this->mod_cafe_img_gallery->get_count() / $display_length);
						$data_content['page_start_index'] = $start_index;

						$this->template->write('content_1', $this->load->view('admin/frm_admin_cafe_gallery', $data_content, true));
						$this->template->render();
					} else 
						redirect(base_url() . 'admin/logout');
				} 
				break;
				case 'logo':{
					
				}
				break;
				case 'cover':{
					
				}
				break;
				case 'contact':{
					
				}
				break;
				case 'add':{					
					$opr = $this->uri->segment(4);					
					switch ($opr){
						case 'submit':{							
							$this->load->model('local/data/cafe/mod_cafe');
							$this->mod_cafe->CAF_TITLE = $this->input->post('ct');
							$this->mod_cafe->CAF_NAME = $this->input->post('cn');
							$this->mod_cafe->MEM_ID = $this->input->post('ow');
							$this->mod_cafe->add();
							
							$this->jscript_redirect(base_url() . 'admin/cafe/list');
						}
						break;
						default:{
							$this->load->model('local/data/mod_member');
							$data_content['member_list_dd'] = $this->mod_member->get_member_list_for_dropdown();

							$this->pre_admin_data();
							$this->template->write('content_1', $this->load->view('admin/frm_admin_cafe_add', $data_content, true));
							$this->template->render();
						}
						break;
					}					
				}
				break;				
			}
			
		}
		function member(){
			
			$opr = $this->uri->segment(3);
			switch ($opr){
				case 'list': default:{
					if ($this->check_admin_session()){
						$this->pre_admin_data('member');
						
						$start_index = $this->input->get('s');
						
						$this->load->library('define/general_define');
						$display_length = $this->general_define->paginate_display_length;
						
						if (empty($start_index))
							$start_index = 1;
							
						$start_index = (($start_index-1) * $display_length);
						
						$this->load->model('local/data/mod_member');
						$data_content['member_list'] = $this->mod_member->get_member_list($start_index, $display_length);
						$data_content['page_count'] = ceil($this->mod_member->get_count() / $display_length);
						$data_content['page_start_index'] = $start_index;
						
						$this->template->write('content_1', $this->load->view('admin/frm_admin_member_list', $data_content, true));
						$this->template->render();	
					} else
						redirect(base_url() . 'admin/logout');			
				}
				break;
				case 'view':{
					if ($this->check_admin_session()){
						// member detail
						$this->load->model('local/data/mod_member');
						$data_content['member'] = $this->mod_member->get_member($this->input->get('m'));
	
						// own cafe list
						$this->load->model('local/data/cafe/mod_cafe');
						$data_content['cafe_list'] = $this->mod_cafe->get_cafe_of_member($this->input->get('m'));
						
						$this->template->write('content_1', $this->load->view('admin/frm_admin_member_view', $data_content, true));
						$this->template->render();
					} else
						redirect(base_url() . 'admin/logout');
				}
				break;
				case 'add':{
					if ($this->check_admin_session()){
						$opr = $this->uri->segment(4);
						switch ($opr){
						case 'submit':{
							$this->template->render();
						}
						break;
						default:{							
							$this->template->render();
						}
						break;
					}
					} else
						redirect(base_url() . 'admin/logout');
				}
				break;
			}
		}
		function article(){
			$opr = $this->uri->segment(3);
			switch ($opr){
				case 'list': default:{						
					$opr = $this->uri->segment(4);
					switch ($opr){
						case 'ajax_remove':{
							$result = false;
							$message = null;
							$redirect = base_url() . 'admin/article/list';
														
							$this->load->model('local/data/article/mod_article');								
							$this->load->library('define/operation_message');								
								
							if ($this->mod_article->remove($this->input->post('a'))){
								$result = true;
								$message = $this->operation_message->msg_remove_success;								
							} else									
								$message = $this->operation_message->msg_remove_fail;							
								
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect' => $redirect)));							
						}
						break;
						case 'ajax_set_feature':{
							$result = false;
							$message = null;
							$redirect = base_url() . 'admin/article/list';
							
							$this->load->library('define/operation_message');
							if ($this->check_admin_session()) {							
								$this->load->model('local/data/article/mod_article');
								
								$state = $this->input->post('s');
								if ($state == 1 || $state == TRUE)
									$state = true;
								else
									$state = false;
								if ($this->mod_article->set_feature($this->input->post('a'), $state)){
									$result = true;
									$message = $this->operation_message->msg_save_success;								
								} else
									$message = $this->operation_message->msg_save_fail;
							} else {
								$result = -2;
								$message = $this->operation_message->msg_relogin;
								$redirect = base_url() . 'admin';
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect' => $redirect)));	
						}
						break;
						case 'ajax_change_state':{
							$result = false;
							$message = null;
							$redirect = base_url() . 'admin/article/list';
							
							$this->load->library('define/operation_message');
							if ($this->check_admin_session()) {							
								$this->load->model('local/data/article/mod_article');
								
								$state = $this->input->post('s');
								if ($state == 1 || $state == TRUE)
									$state = true;
								else
									$state = false;
								
								if ($this->mod_article->set_published('"' . $this->input->post('a') . '"', $state)){
									$result = true;
									$message = $this->operation_message->msg_save_success;								
								} else
									$message = $this->operation_message->msg_save_fail;
							} else {
								$result = -2;
								$message = $this->operation_message->msg_relogin;
								$redirect = base_url() . 'admin';
							}
							
							print_r(json_encode(array('result' => $result,
														'message' => $message,
														'redirect' => $redirect)));	
						}
						break;
						default:{
							if ($this->check_admin_session() === 1){
								$this->pre_admin_data('article');
							
								$start_index = $this->input->get('p');
								$display_length = 10;	
									
								if (empty($start_index))
									$start_index = 0;
								else{
									$start_index = ($start_index -1) * $display_length;
									if ($start_index < 0)
										$start_index = 0;
								}					
									
								$this->load->model('local/data/article/mod_article');
								$this->load->model('local/data/article/mod_article_category');
								$data_content['data'] = $this->mod_article->get_article_list($display_length, $start_index);										
								$data_content['max_count'] = ceil($this->mod_article->get_count() / $display_length);
								$data_content['category'] = $this->mod_article_category->get_category_for_dropdownlist();			
																	
								$this->template->write('content_1', $this->load->view('admin/frm_admin_article', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'admin/logout');								
						}
						break;
					}						
				}
				break;
				case 'add':{
						
						$opr = $this->uri->segment(4);
						switch ($opr){
							case 'submit':{
								if ($this->check_admin_session()){
									$is_feature = false;								
									if ($this->input->post('af'))
										$is_feature = true;
									
									$this->load->model('local/data/article/mod_article');
									$this->mod_article->ART_TITLE = $this->input->post('at');
									$this->mod_article->ART_TEXT = $this->input->post('ac');
									$this->mod_article->ART_TYPE_ID = $this->input->post('atp');
									$this->mod_article->ART_CAT_ID = $this->input->post('acat');
									$this->mod_article->ART_TAG = $this->input->post('atag');
									$this->mod_article->ART_FEATURE = $is_feature;
									$this->mod_article->ADM_ID = $this->session->userdata('aid');
									
									$upload_pass = true;
									if (isset($_FILES['athumb']) && !empty($_FILES['athumb']['name'])){																	
										$file = $_FILES['athumb'];
										$this->load->library('imageupload');
										$this->load->library('define/image_upload_define');
										$this->load->library('define/directory_path');
							
										$this->imageupload->target_directory = $this->directory_path->img_upload_article_thumb_path;
										$this->imageupload->max_image_height = $this->image_upload_define->thumb_article_img_limit_height;
										$this->imageupload->max_image_width = $this->image_upload_define->thumb_article_img_limit_width;
										$this->imageupload->max_image_size = $this->image_upload_define->thumb_article_img_limit_size;
										$this->imageupload->image_extensions = $this->image_upload_define->thumb_article_img_extensions;
										$this->imageupload->encode_filename = false;
												
										if (!$this->imageupload->upload($file)){
											$upload_pass = false;
											$message = $this->imageupload->error_message;
										}
							
										$this->mod_article->ART_IMG_THUMB = $file['name'];
									}
									
									if ($this->mod_article->add()){
										$this->jscript_redirect(base_url() . 'admin/article/list');
									}else{
										$this->load->library('define/operation_message');
										$this->jscript_redirect(base_url() . 'admin/article/list', $this->operation_message->msg_save_fail);									
									}
								} else
									redirect(base_url() . 'admin/logout');
								
							}
							break;
							default:{
								
								if ($this->check_admin_session()){
									$this->pre_admin_data('article/add');
							
									$this->load->model('local/data/article/mod_article_category');
									$data_content['category'] = $this->mod_article_category->get_category_for_dropdownlist();
																		
									$this->template->write('content_1', $this->load->view('admin/frm_admin_article_add', $data_content, true));
									$this->template->render();		
								} else 
									redirect(base_url() . 'admin/logout');
							}
							break;
						}						
					}	
					break;			
				case 'view':{
						
						$opr = $this->uri->segment(4);
						switch ($opr){
							case 'submit':{
								
								if ($this->check_admin_session()){
									$is_feature = false;								
									if ($this->input->post('af'))
										$is_feature = true;
									
									$this->load->model('local/data/article/mod_article');
									$this->mod_article->ART_TITLE = $this->input->post('at');
									$this->mod_article->ART_TEXT = $this->input->post('ac');
									$this->mod_article->ART_TYPE_ID = $this->input->post('atp');
									$this->mod_article->ART_CAT_ID = $this->input->post('acat');
									$this->mod_article->ART_TAG = $this->input->post('atag');
									$this->mod_article->ART_FEATURE = $is_feature;
																	
									$upload_pass = true;
									if (isset($_FILES['athumb']) && !empty($_FILES['athumb']['name'])){																	
										$file = $_FILES['athumb'];
										$this->load->library('imageupload');
										$this->load->library('define/image_upload_define');
										$this->load->library('define/directory_path');
							
										$this->imageupload->target_directory = $this->directory_path->img_upload_article_thumb_path;
										$this->imageupload->max_image_height = $this->image_upload_define->thumb_article_img_limit_height;
										$this->imageupload->max_image_width = $this->image_upload_define->thumb_article_img_limit_width;
										$this->imageupload->max_image_size = $this->image_upload_define->thumb_article_img_limit_size;
										$this->imageupload->image_extensions = $this->image_upload_define->thumb_article_img_extensions;
										$this->imageupload->encode_filename = false;
												
										if (!$this->imageupload->upload($file)){
											$upload_pass = false;
											$message = $this->imageupload->error_message;
										}
							
										$this->mod_article->ART_IMG_THUMB = $file['name'];
									}
									
									if ($this->mod_article->edit($this->input->post('acd'))){												
										$this->jscript_redirect(base_url() . 'admin/article/list');
									}else{
										$this->load->library('define/operation_message');
										$this->jscript_redirect(base_url() . 'admin/article/list', $this->operation_message->msg_save_fail);									
									}	
								} else
									redirect(base_url() . 'admin/logout');						
							}
							break;
							default:{
								if ($this->check_admin_session()){
									$this->pre_admin_data();
									
									$this->load->model('local/data/article/mod_article');
									$data_content['data'] = $this->mod_article->get_single_article($opr);
							
									$this->load->model('local/data/article/mod_article_category');
									$data_content['category'] = $this->mod_article_category->get_category_for_dropdownlist();					

									$this->template->write('content_1', $this->load->view('admin/frm_admin_article_view', $data_content, true));
									$this->template->render();
								} else
									redirect(base_url() . 'admin/logout');
							}
							break;
						}
					}	
					break;				
			}
			
		}
		function order(){
			$opr = $this->uri->segment(3);
			switch ($opr){
				case 'new':{
					
					$opr = $this->uri->segment(4);
					switch ($opr){
						case 'view':{
							if ($this->check_admin_session() === 1){
								$order_code = $this->input->get('o');
								
								$this->load->model('local/data/mod_package_order');							
								$data_content['order'] = $this->mod_package_order->get_single_order($order_code);
								
								$this->load->model('local/data/cafe/mod_cafe_expire');
								$data_content['last_order'] = $this->mod_cafe_expire->get_current_package_state($data_content['order']['CAF_ID'], false, true);													
								
								$this->template->write('content_1', $this->load->view('admin/frm_admin_order_new_view', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
						case 'submit':{
							if ($this->check_admin_session() === 1){
								$opr = $this->input->post('opr');
								$order_code = $this->input->post('o');							
								
								print 'opr = ' . $opr . '<br />o = ' . $order_code;
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
						default:{
							
							if ($this->check_admin_session() === 1){
								$start_index = $this->input->get('p');
									
								$this->load->library('define/general_define');
								$display_length = $this->general_define->paginate_display_length;
									
								if (empty($start_index))
									$start_index = 1;
								
								$start_index = (($start_index-1) * $display_length);
									
								$this->load->library('define/general_define');
								$display_length = $this->general_define->paginate_display_length;
									
								$this->load->model('local/data/mod_package_order');
								$data_content['new_order_list'] = $this->mod_package_order->get_new_order_list($start_index, $display_length);
								$data_content['page_count'] = ceil($this->mod_package_order->get_new_order_count() / $display_length);
								$data_content['page_start_index'] = $start_index;						
									
								$this->template->write('content_1', $this->load->view('admin/frm_admin_order_new', $data_content, true));
								$this->template->render();
							} else 
								redirect(base_url() . 'admin/logout');							
						}
						break;
					}					
				}
				break;
				case 'payment':{
					
					$opr = $this->uri->segment(4);
					switch ($opr){
						case 'view':{
							if ($this->check_admin_session() === 1) {
								$order_code = $this->input->get('o');
								
								$this->load->model('local/data/mod_package_order');							
								$data_content['order'] = $this->mod_package_order->get_single_order($order_code);																				
															
								$this->template->write('content_1', $this->load->view('admin/frm_admin_order_payment_view', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
						default:{
							if ($this->check_admin_session() === 1){
								$start_index = $this->input->get('p');
									
								$this->load->library('define/general_define');
								$display_length = $this->general_define->paginate_display_length;
									
								if (empty($start_index))
									$start_index = 1;
								
								$start_index = (($start_index-1) * $display_length);
									
								$this->load->library('define/general_define');
								$display_length = $this->general_define->paginate_display_length;
									
								$this->load->model('local/data/mod_package_order');
								$data_content['payment_order_list'] = $this->mod_package_order->get_payment_order_list($start_index, $display_length);
								$data_content['page_count'] = ceil($this->mod_package_order->get_payment_order_count() / $display_length);
								$data_content['page_start_index'] = $start_index;						
									
								$this->template->write('content_1', $this->load->view('admin/frm_admin_order_payment', $data_content, true));
								$this->template->render();	
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
					}					
				}
				break;
				case 'list': default:{
					if ($this->check_admin_session() === 1){
						$this->pre_admin_data('order');									
						
						$this->template->render();
					} else
						redirect(base_url() . 'admin/logout');			
				}
				break;
			}
		}
	
		function banner(){
			$opr = $this->uri->segment(3);
			switch ($opr){
				case 'view':{
					$opr = $this->uri->segment(4);
					switch ($opr){
						case 'submit-u':{
							if ($this->check_admin_session() === 1){										
								$this->load->model('local/data/etc/mod_banner');
								$this->mod_banner->BAN_URL = $this->input->post('durl');
								$this->mod_banner->BAN_S_DATETIME = $this->input->post('dps');
								$this->mod_banner->BAN_E_DATETIME = $this->input->post('dpe');
								$this->mod_banner->BAN_WIDTH = $this->input->post('dwidth');
								$this->mod_banner->BAN_HEIGHT = $this->input->post('dheight');
								$this->mod_banner->BAN_P_ID = $this->input->post('dt');
													
								$message = null;
								if (!$this->mod_banner->edit_banner($this->input->post('did'))){
									$this->load->library('define/operation_message');
									$message = $this->operation_message->msg_save_fail;											
								}
										
								$this->jscript_redirect(base_url() . 'admin/banner', $message);																				
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
						case 'submit-c':{
							if ($this->check_admin_session() === 1){								
								$this->load->model('local/data/etc/mod_banner');
								$this->mod_banner->BAN_S_DATETIME = $this->input->post('dps');
								$this->mod_banner->BAN_E_DATETIME = $this->input->post('dpe');									
								$this->mod_banner->BAN_P_ID = $this->input->post('dt');
								$this->mod_banner->BAN_CODE = $this->input->post('cb');
										
								$message = null;
								if (!$this->mod_banner->edit_codeblock($this->input->post('did'))){
									$this->load->library('define/operation_message');
									$message = $this->operation_message->msg_save_fail;
								} 

								$this->jscript_redirect(base_url() . 'admin/banner', $message);										
							} else
								redirect(base_url() . 'admin/logout');			
						}
						break;
						default:{
							if ($this->check_admin_session() == 1){
								$this->pre_admin_data('banner');
											
								$id = $this->input->get('i');
											
								$this->load->model('local/data/etc/mod_banner');
								$this->load->model('local/data/etc/mod_banner_position');
								$data_content['item'] = $this->mod_banner->get_single_banner($id);
								$data_content['banner_position'] = $this->mod_banner_position->get_banner_position_for_dropdown();																	
											
								$this->template->write('content_1', $this->load->view('admin/frm_admin_banner_view', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'admin/logout');		
						}
						break;
					}							
				}
				break;
				case 'add':{
					$opr = $this->uri->segment(4);							
					switch ($opr){
						case 'submit-u':{
							if ($this->check_admin_session() === 1){
								$file = $_FILES['fc'];									
											
								if (!empty($file['name'])) {
											
									$this->load->library('define/operation_message');
									$this->load->library('define/directory_path');
									$this->load->library('define/image_upload_define');
									$this->load->library('imageupload');
													
									$this->imageupload->target_directory = $this->directory_path->img_upload_banner_path;
									$this->imageupload->max_image_height = $this->image_upload_define->banner_img_limit_height;
									$this->imageupload->max_image_width = $this->image_upload_define->banner_img_limit_width;
									$this->imageupload->max_image_size = $this->image_upload_define->banner_img_limit_size;
									$this->imageupload->image_extensions = $this->image_upload_define->banner_img_extensions;
									$this->imageupload->prefix = 'b';
									$this->imageupload->encode_filename = true;
								
									$message = null;
									if (!$this->imageupload->upload($file)){
										$message = $this->imageupload->error_message;
									} else {
										$this->load->model('local/data/etc/mod_banner');
										$this->mod_banner->BAN_URL = $this->input->post('durl');
										$this->mod_banner->BAN_S_DATETIME = $this->input->post('dps');
										$this->mod_banner->BAN_E_DATETIME = $this->input->post('dpe');											
										$this->mod_banner->BAN_IMG = $this->imageupload->encode_filename($file['name']);
										$this->mod_banner->BAN_WIDTH = $this->input->post('dwidth');
										$this->mod_banner->BAN_HEIGHT = $this->input->post('dheight');
										$this->mod_banner->BAN_P_ID = $this->input->post('dt');
										$this->mod_banner->BAN_STATE = $this->input->post('ds');
													
										if (!$this->mod_banner->add_banner())
											$message = $this->operation_message->msg_save_fail;
									}
										
									if (is_null($message))
										$this->jscript_redirect(base_url() . 'admin/banner');
									else
										$this->jscript_redirect(base_url() . 'admin/banner', $message);
								} else
									$this->jscript_redirect(base_url() . 'admin/banner', 'ยังไม่ได้อัพรูปภาพ');											
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
						case 'submit-c':{
							if ($this->check_admin_session() === 1){								
								$this->load->model('local/data/etc/mod_banner');
								$this->mod_banner->BAN_S_DATETIME = $this->input->post('dps');
								$this->mod_banner->BAN_E_DATETIME = $this->input->post('dpe');									
								$this->mod_banner->BAN_P_ID = $this->input->post('dt');
								$this->mod_banner->BAN_CODE = $this->input->post('cb');
																				
								if ($this->mod_banner->add_codeblock())
									$this->jscript_redirect(base_url() . 'admin/banner');
								else {
									$this->load->library('define/operation_message');
									$this->jscript_redirect(base_url() . 'admin/banner', $this->operation_message->msg_save_fail);
								}
							} else
								redirect(base_url() . 'admin/logout');									
						}
						break;
						default:{
							if ($this->check_admin_session() === 1){
								$this->pre_admin_data('banner');
											
								$this->load->model('local/data/etc/mod_banner_position');
								$data_content['banner_list_dd'] = $this->mod_banner_position->get_banner_position_for_dropdown();
											
								$this->template->write('content_1', $this->load->view('admin/frm_admin_banner_add', $data_content, true));
								$this->template->render();
							} else 
								redirect(base_url() . 'admin/logout');																				
						}
						break;
					}							
				}
				break;
				case 'remove':{
					if ($this->check_admin_session()){
						$this->load->model('local/data/etc/mod_banner');
																				
						if ($this->mod_banner->remove($this->input->post('i')))
							$this->jscript_redirect(base_url() . 'admin/banner');
						else {
							$this->load->library('define/operation_message');
							$this->jscript_redirect(base_url() . 'admin/banner', $this->operation_message->msg_remove_fail);	
						}
					} else
						redirect(base_url() . 'admin/logout');
				}
				break;
				case 'ajax_change_state':{
					$result = 0;
					$message = null;
					$redirect_url = null;

					$this->load->library('define/operation_message');
					if ($this->check_admin_session() === 1){
						$this->load->model('local/data/etc/mod_banner');								
						if ($this->mod_banner->set_banner_state($this->input->post('i'), $this->input->post('s')))
							$result = 1;
						else
							$message = $this->operation_message->msg_save_fail;
					} else {
						$result = -1;								
						$message = $this->operation_message->msg_relogin;
						$redirect_url = base_url() . 'admin/logout';
					}

					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
				}
				break;
				default:{
					if ($this->check_admin_session() === 1){
						$this->pre_admin_data('banner');
								
						$start_index = $this->input->get('p');
						if (empty($start_index))
							$start_index = 1;

						$this->load->library('define/general_define');
						$display_length = $this->general_define->paginate_display_length;

						$this->load->model('local/data/etc/mod_banner');
						$data_content['data'] = $this->mod_banner->get_banner_list($start_index, $display_length);
						$data_content['page_count'] = ceil($this->mod_banner->get_count() / $display_length);
						$data_content['page_start_index'] = $start_index;							

						$this->template->write('content_1', $this->load->view('admin/frm_admin_banner', $data_content, true));
						$this->template->render();
					} else
						redirect(base_url() . 'admin/logout');							
				}
				break;
			}		
		}
		function carousel(){
			$opr = $this->uri->segment(3);
						
			switch ($opr){
				case 'add':{
					$opr = $this->uri->segment(4);							
					switch ($opr){
						case 'submit':{
							if ($this->check_admin_session() === 1){									
								$file = $_FILES['fc'];

								if (!empty($file['name'])) {
									$desc = $this->input->post('desc');
									$start_date = $this->input->post('dps');
									$end_date = $this->input->post('dpe');

									$this->load->library('define/operation_message');
									$this->load->library('define/directory_path');
									$this->load->library('define/image_upload_define');
									$this->load->library('imageupload');									
																							
									$this->imageupload->target_directory = $this->directory_path->img_upload_carousel_path;
									$this->imageupload->max_image_height = $this->image_upload_define->carousel_img_limit_height;
									$this->imageupload->max_image_width = $this->image_upload_define->carousel_img_limit_width;
									$this->imageupload->max_image_size = $this->image_upload_define->carousel_img_limit_size;
									$this->imageupload->image_extensions = $this->image_upload_define->carousel_img_extensions;
									$this->imageupload->encode_filename = false;
																			
									$message = null;
									if (!$this->imageupload->upload($file)){										
										$message = $this->imageupload->error_message;
									} else {
										$this->load->model('local/data/etc/mod_carousel');
										$this->mod_carousel->CAR_S_DATETIME = $start_date;									
										$this->mod_carousel->CAR_E_DATETIME = $end_date;
										$this->mod_carousel->CAR_DESC = $desc;
										$this->mod_carousel->CAR_IMG = $file['name'];
										if (!$this->mod_carousel->add())
											$message = $this->operation_message->msg_save_fail;											
									}									
												
									$this->jscript_redirect(base_url() . 'admin/carousel', $message);																																
								}
							} else
								redirect(base_url() . 'admin/logout');
						}
						break;
						default:{
							if ($this->check_admin_session() === 1){
								$this->pre_admin_data('carousel');
								
								$data_content = array();
								$this->template->write('content_1', $this->load->view('admin/frm_admin_carousel_add', $data_content, true));
								$this->template->render();
							} else 
								redirect(base_url() . 'admin/logout');										
						}
						break;
					}							
				}
				break;
				case 'remove':{
					if ($this->check_admin_session() === 1){
						$carousel_id = $this->input->post('i');
								
						$this->load->model('local/data/etc/mod_carousel');						
						if ($this->mod_carousel->remove($carousel_id))		
							$this->jscript_redirect(base_url() . 'admin/carousel/');							
						else
							$this->jscript_redirect(base_url() . 'admin/carousel/', $this->operation_message->msg_save_failed);
					} else 
						redirect(base_url() . 'admin/logout');								
				}
				break;
				case 'view':{
					$opr = $this->uri->segment(4);
					switch ($opr){
						case 'submit':{
							if ($this->check_admin_session() === 1){
								$start_date = $this->input->post('dps');
								$end_date = $this->input->post('dpe');
								$carousel_id = $this->input->post('cid');
								$desc = $this->input->post('desc');
													
								$this->load->model('local/data/etc/mod_carousel');
								$this->mod_carousel->CAR_S_DATETIME = $start_date;
								$this->mod_carousel->CAR_E_DATETIME = $end_date;
								$this->mod_carousel->CAR_DESC = $desc;
																										
								if ($this->mod_carousel->edit($carousel_id))
									$this->jscript_redirect(base_url() . 'admin/carousel/view?i=' . $carousel_id);
								else
									$this->jscript_redirect(base_url() . 'admin/carousel/view?i=' . $carousel_id, $this->operation_message->msg_save_failed);
							} else
								redirect(base_url() . 'admin/logout');	
						}
						break;
						default:{
							if ($this->check_admin_session() === 1){
								$this->pre_admin_data('carousel');			
								$carousel_id = $this->input->get('i');
											
								$this->load->model('local/data/etc/mod_carousel');
								$data_content['data'] = $this->mod_carousel->get_single_carousel($carousel_id);
		
								$this->template->write('content_1', $this->load->view('admin/frm_admin_carousel_view', $data_content, true));
								$this->template->render();
							} else
								redirect(base_url() . 'admin/logout');		
						}
						break;
					}					
				}
				break;
				case 'ajax_change_state':{
					$result = 0;
					$message = null;
					$redirect_url = null;

					$this->load->library('define/operation_message');
					if ($this->check_admin_session() === 1){
						$this->load->model('local/data/etc/mod_carousel');								
						if ($this->mod_carousel->set_carousel_state($this->input->post('i'), $this->input->post('s')))
							$result = 1;
						else
							$message = $this->operation_message->msg_save_fail;
					} else {
						$result = -1;								
						$message = $this->operation_message->msg_relogin;
						$redirect_url = base_url() . 'admin/logout';
					}

					print_r(json_encode(array('result' => $result,
												'message' => $message,
												'redirect_url' => $redirect_url)));
				}
				default:{
					if ($this->check_admin_session() === 1){
						$this->pre_admin_data('carousel');
						
						$start_index = $this->input->get('p');
						if (empty($start_index))
							$start_index = 1;
						
						$this->load->library('define/general_define');
						$display_length = $this->general_define->paginate_display_length;
						
						$this->load->model('local/data/etc/mod_carousel');
						$data_content['data'] = $this->mod_carousel->get_carousel_list(true);
						$data_content['page_count'] = ceil($this->mod_carousel->get_count() / $display_length);
						$data_content['page_start_index'] = $start_index;								

						$this->template->write('content_1', $this->load->view('admin/frm_admin_carousel', $data_content, true));
						$this->template->render();
					} else
						redirect(base_url() . 'admin/logout');
				}
				break;
			}
		}
		
		// authen section
		function login(){
			$username = $this->input->post('u');
			$password = $this->input->post('p');
			
			$this->load->model('local/data/mod_admin');
			$admin = $this->mod_admin->authen($username, $password);
			
			if (!is_null($admin)){
				$this->session->set_userdata(array('aid' => $admin['ADM_ID'],
													'name' => $admin['ADM_USERNAME'],
													'admin_logged_in' => true));
				$this->jscript_redirect(base_url() . 'admin/main');
			} else {
				$this->jscript_redirect(base_url() . 'admin/');
			}
						
		}
		function logout(){
			parent::logout();
			redirect(base_url() . 'admin');
		}
	}
?>
